//
//  OrderFooterCell.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/8/2.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit

protocol OrderFooterCellDeleage : NSObjectProtocol {
    
    func orderFooterCellBtnDidTap(str: String)
    
}

class OrderFooterCell: BaseTableViewCell {
    
    lazy var buttonTittles = [String]()
    
    @IBOutlet weak var rightBtn: UIButton!
    
    weak var delegate : OrderFooterCellDeleage?

    
    @IBOutlet weak var middleBtn: UIButton!
    

    @IBOutlet weak var leftBtn: UIButton!
    
    @IBOutlet weak var topLabel: UILabel!
    
    
    
    var model : OneOrderModel!{
        
        
        didSet{
            
            buttonTittles.removeAll()
            
            if model.if_cancel == "1" {
                buttonTittles.append("取消订单")
            }
            
            
            if model.if_delete == "1" {
                buttonTittles.append("删除订单")
            }
            
            
            if model.if_payment == "1" {
                buttonTittles.append("去支付")
            }
            
            
            if model.if_receive == "1" {
                buttonTittles.append("确认收货")
            }
            
            
            if model.if_deliver == "1" {
                buttonTittles.append("查看物流")
            }
            
            
            if model.if_evaluation == "1" {
                buttonTittles.append("评价订单")
            }
            
            
            if model.if_refund_cancel == "1" {
                buttonTittles.append("订单退款")
            }
            
            
            switch buttonTittles.count {
            case 0:
                leftBtn.isHidden = true
                middleBtn.isHidden = true
                rightBtn.isHidden = true
                
            case 1:
                leftBtn.isHidden = true
                middleBtn.isHidden = true
                rightBtn.isHidden = false
                
            case 2:
                leftBtn.isHidden = true
                middleBtn.isHidden = false
                rightBtn.isHidden = false
                
            case 3:
                leftBtn.isHidden = false
                middleBtn.isHidden = false
                rightBtn.isHidden = false
                
            default:
                leftBtn.isHidden = true
                middleBtn.isHidden = true
                rightBtn.isHidden = true
            }
            
            
            if rightBtn.isHidden == false{
                
                rightBtn.setTitle(buttonTittles[0], for: .normal)
                
            }
            
            if middleBtn.isHidden == false{
                
                middleBtn.setTitle(buttonTittles[1], for: .normal)
                
            }
            
            
            
            if leftBtn.isHidden == false{
                
                leftBtn.setTitle(buttonTittles[2], for: .normal)
                
            }
            
            let str = "共\(model.goods_count)件商品 合计: ¥\(model.order_amount) " + "(" + "运费¥\(model.shipping_fee)"  + ")"
            
            
            topLabel.attributedText = attributedString2(str: str, font: UIFont.systemFont(ofSize: 16))
            
            
        }
        
        
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        leftBtn.addCornerAndBorder(borderColor: UIColor.red, borderWidth: 0.6, masksToBounds: true, cornerRadius: 5)
         middleBtn.addCornerAndBorder(borderColor: UIColor.red, borderWidth: 0.6, masksToBounds: true, cornerRadius: 5)
         rightBtn.addCornerAndBorder(borderColor: UIColor.red, borderWidth: 0.6, masksToBounds: true, cornerRadius: 5)
        
        
        // Initialization code
    }
    
    
    @IBAction func rightBtnDidTap(_ sender: UIButton) {
        
        delegate?.orderFooterCellBtnDidTap(str: sender.currentTitle!)
    }
    
    
    
    @IBAction func middleBtnDidTap(_ sender: UIButton) {
        
        delegate?.orderFooterCellBtnDidTap(str: sender.currentTitle!)

    }
    
    
    @IBAction func leftBtnDidTap(_ sender: UIButton) {
        
        delegate?.orderFooterCellBtnDidTap(str: sender.currentTitle!)

    }
    
    
    
    func attributedString2(str: String, font: UIFont) -> NSAttributedString? {
        
        
        let patten0 = "[0-9]{1,15}"
        //创建正则表达式对象
        let patten1 = "[.]"
        
        //        let patten2 = "[¥]"    + "|" +  patten2
        
        let patten = patten0 + "|" + patten1
        
        //caseInsensitive  忽略大小写  最常用
        //dotMatchesLineSeparators 允许 . 匹配任意字符，包括回车换行
        let regex = try! NSRegularExpression(pattern: patten, options: NSRegularExpression.Options.caseInsensitive)
        
        //根据规则匹配指定字符串中第一个符合规则的字符串，并且返回一个NSTextCheckingResult
        let res1 = regex.matches(in: str, options: NSRegularExpression.MatchingOptions.init(rawValue: 0), range: NSRange(location: 0, length: str.characters.count))
        
        // 根据原有字符串生成属性字符串
        let strM = NSMutableAttributedString(string: str)
        
        
        strM.addAttribute(NSForegroundColorAttributeName, value: UIColor.black, range:NSMakeRange(0, str.characters.count) )
        
        for temp in res1{
            
            
            strM.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range:temp.range )
            
            
            strM.addAttribute(NSFontAttributeName, value: font, range:temp.range)
            
            
        }
        
        
        
        
        return strM
        
        
        
        
        
        
    }
    
    

    
    
    
      

    

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        

    }
    
}
